extern crate fejpsl_contract;
use fejpsl_contract::china::China;

pub async fn get_province_list() -> Option<Vec<China>> {
    let client = super::db::connect().await.unwrap();
    let mut vec_province = Vec::new();
    let rows = &client.query("SELECT code id,name FROM public.t_china where pid='0' order by code",
                             &[]).await.unwrap();
    for row in rows {
        let province = China {
            id:                 row.get(0),
            name:          row.get(1),
        };
        vec_province.push(province);
    }
    return Some(vec_province);
}
pub async fn get_city_list(id:&String) -> Option<Vec<China>> {
    let client = super::db::connect().await.unwrap();
    let mut vec_city = Vec::new();
    let rows = &client.query("SELECT code id,name FROM public.t_china where code=pid and pid=$1 order by code",
    &[&id]).await.unwrap();
    let count  = rows.len();
    if count>0 {
        let rows = &client.query("SELECT code id,name FROM public.t_china where pid=$1 and cid=$1 order by code",
                                 &[&id]).await.unwrap();
        for row in rows {
            let city = China {
                id: row.get(0),
                name: row.get(1),
            };
            vec_city.push(city);
        }
    }else {
        let rows = &client.query("SELECT code id,name FROM public.t_china where pid=$1 and cid='0' order by code",
                                 &[&id]).await.unwrap();
        for row in rows {
            let city = China {
                id: row.get(0),
                name: row.get(1),
            };
            vec_city.push(city);
        }
    }
    return Some(vec_city);
}
